package com.sszh.base.utils;

import org.springframework.stereotype.Service;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author WangJianGuo
 * @Title: MyFileUtils
 * @Package com.sszh.base.utils
 * @Description: 文件复制工具类
 * @date 2018-7-18 17:41
 */
@Service
public class MyFileUtils {

    public void copyDir(String oldPath, String newPath) {

        File file = new File(oldPath);

        String[] filePath = file.list();

        if (!(new File(newPath)).exists()) {
            (new File(newPath)).mkdir();
        }

        for (int i = 0; i < filePath.length; i++) {

            if ((new File(oldPath + File.separator + filePath[i])).isDirectory()) {
                copyDir(oldPath + File.separator + filePath[i], newPath + File.separator + filePath[i]);
            }

            if (new File(oldPath + File.separator + filePath[i]).isFile()) {
                copyFile(oldPath + File.separator + filePath[i], newPath + File.separator + filePath[i]);
            }

        }
    }

    public void copyFile(String oldPath, String newPath) {

        File oldFile = new File(oldPath);

        File file = new File(newPath);

        FileInputStream in = null;

        try {

            in = new FileInputStream(oldFile);
            FileOutputStream out = new FileOutputStream(file);

            byte[] buffer = new byte[2097152];

            while ((in.read(buffer)) != -1) {
                out.write(buffer);
            }
        } catch (Exception e) {

        }
    }


    public void zip(String zipFileName, String sourceFileName) {

        //创建zip输出流
        ZipOutputStream out = null;
        try {
            out = new ZipOutputStream(new FileOutputStream(zipFileName));
            //创建缓冲输出流
            BufferedOutputStream bos = new BufferedOutputStream(out);

            File sourceFile = new File(sourceFileName);

            //调用函数
            compress(out, bos, sourceFile, sourceFile.getName());

            bos.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public void compress(ZipOutputStream out, BufferedOutputStream bos, File sourceFile, String base) {
        //如果路径为目录（文件夹）
        try {

            if (sourceFile.isDirectory()) {

                //取出文件夹中的文件（或子文件夹）
                File[] flist = sourceFile.listFiles();

                //如果文件夹为空，则只需在目的地zip文件中写入一个目录进入点
                if (flist.length == 0) {

                    out.putNextEntry(new ZipEntry(base + "/"));

                    //如果文件夹不为空，则递归调用compress，文件夹中的每一个文件（或文件夹）进行压缩
                } else {
                    for (int i = 0; i < flist.length; i++) {
                        compress(out, bos, flist[i], base + "/" + flist[i].getName());
                    }
                }
                //如果不是目录（文件夹），即为文件，则先写入目录进入点，之后将文件写入zip文件中
            } else {
                out.putNextEntry(new ZipEntry(base));
                FileInputStream fos = new FileInputStream(sourceFile);
                BufferedInputStream bis = new BufferedInputStream(fos);

                int tag;
                //将源文件写入到zip文件中
                while ((tag = bis.read()) != -1) {
                    bos.write(tag);
                }
                bis.close();
                fos.close();

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
